package ext.generic.erp.manually;

import java.util.Locale;

import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationStatus;

import ext.generic.erp.rule.BussinessRule;
import ext.generic.erp.util.PDMIntegrationLogUtil;
import ext.tianma.part.forms.TMTypeUtil;
import wt.fc.Persistable;
import wt.fc.WTReference;
import wt.part.WTPart;

public class MateriaModificationActionValidator extends DefaultUIComponentValidator {
	private static String clazz = MdmManuallyPublishActionValidator.class.getName();

	private static final String D_VIEW = "Design";

	public MateriaModificationActionValidator() {

	}

	public UIValidationStatus preValidateAction(UIValidationKey uivalidationkey,
			UIValidationCriteria uivalidationcriteria) {
		UIValidationStatus uivalidationstatus = UIValidationStatus.HIDDEN;

		WTReference wtreference = uivalidationcriteria.getContextObject();

		if (wtreference != null) {
			Persistable persistable = wtreference.getObject();

			try {
				if (persistable != null && persistable instanceof WTPart) {
					WTPart wtpart = (WTPart) persistable;
					// 修改原因：ERP集成中“Design”视图的零部件不用发到中间表
					if (wtpart.getViewName().equals(D_VIEW)) {

						// 是否显示手工发布菜单
						boolean result = showMateriaModificationMenu(wtpart);

						// PDMIntegrationLogUtil.printLog(clazz, "showManuallyPublishMenu : " + result )
						// ;

						if (result) {
							uivalidationstatus = UIValidationStatus.ENABLED;
						} else {
							uivalidationstatus = UIValidationStatus.HIDDEN;
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return uivalidationstatus;
	}

	private boolean showMateriaModificationMenu(WTPart part) {
		String partstate = part.getState().getState().getDisplay(Locale.CHINA);
		if (TMTypeUtil.isRawMaterial(part)) {
			if ("正在工作".equals(partstate) || "新品".equals(partstate) || "批准".equals(partstate)
					|| "量产发布".equals(partstate)) {
				return true;
			}
		} else if (TMTypeUtil.isSemiFinishedGood(part)) {
			if ("正在工作".equals(partstate) || "新品".equals(partstate) || "试产".equals(partstate) || "风险批量".equals(partstate)
					|| "小批量".equals(partstate) || "量产发布".equals(partstate)) {
				return true;
			}
		} else if (TMTypeUtil.isFinishedGood(part)) {
			if ("正在工作".equals(partstate) || "新品".equals(partstate) || "试产".equals(partstate) || "风险批量".equals(partstate)
					|| "小批量".equals(partstate) || "量产发布".equals(partstate)) {
				return true;
			}
		}
		return false;
	}
}
